package com.demo.chapter10optional;

import java.util.Optional;

//两个Optional对象的组合
public class OperationsWithOptional {

	public static void main(String... args) {
		System.out.println(max(Optional.of(3), Optional.of(5)));
		System.out.println(max(Optional.empty(), Optional.of(5)));
	}

	public static final Optional<Integer> max(Optional<Integer> i, Optional<Integer> j) {
		//不用分别对两个Optional对象进行非空判断
		//if(i.isPresent()&&j.isPresent())
		return i.flatMap(a -> j.map(b -> Math.max(a, b)));
	}

}
